package com.gtfrank.web.servlet.day3;

import com.gtfrank.web.servlet.pojo.User;
import com.gtfrank.web.servlet.service.IUserService;
import com.gtfrank.web.servlet.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: LoginServlet
 * @PackageName:com.gtfrank.web.servlet.day2
 * @Description:
 * @Author: frank
 * @Date: 2021/4/28 16:19
 * @Version v1.0
 */
@WebServlet(value = "/user/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        IUserService userService = new UserServiceImpl();
        /**
         * 登录成功显示用户登录成功，失败显示失败
         */
        try {
        String name = req.getParameter("userName");
        String password = req.getParameter("password");
        User user = userService.login(name, password);
            if(user !=null){
               req.getRequestDispatcher("/user/showUserListServlet").forward(req,resp);
            }else{
                req.getRequestDispatcher("/servlet/day3/login.html").forward(req,resp);
            }
        }catch (Exception e){
            e.printStackTrace();
            req.getRequestDispatcher("/servlet/day3/login.html").forward(req,resp);
        }

    }
}
